<template>
  <div class="employees-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">
            <login-setting :user-detail="userDetail" @saveEmpDetailByIdFn="saveEmpDetailByIdFn" />
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <userInfo ref="userInfo" :depar-tree-list="deparTreeList" @updateUserInfoEV="updateUserInfoEV" />
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <post-info />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import postInfo from './components/post-info.vue'
import userInfo from './components/user-info.vue'
import loginSetting from './components/login-setting.vue'
import { getUserDetailByIdAPI, saveEmpDetailByIdAPI } from '@/api/user'
import { getDepartmentsListAPI } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
export default {
  name: 'EmployeesDetail',
  components: {
    postInfo,
    userInfo,
    loginSetting
  },
  data() {
    return {
      userDetail: {},
      deparTreeList: []
    }
  },
  created() {
    this.getUserDetailFn()
    this.getDepartmentsListFn()
  },
  methods: {
    // 获取员工基本信息功能
    async getUserDetailFn() {
      const res = await getUserDetailByIdAPI(this.$route.query.id)
      // 方式2. 直接赋值
      this.$refs.userInfo.userInfo = res.data
      // 方式1. 传统方式 父传子数据(然后子里把props的值赋予给data)
      this.userDetail = res.data
    },
    // 获取部门列表
    async getDepartmentsListFn() {
      const res = await getDepartmentsListAPI()
      // console.log(res.data)
      this.deparTreeList = tranListToTreeData(res.data.depts, '')
      // console.log(this.deparTreeList)
    },
    // 更新-员工详情(姓名/密码)
    async saveEmpDetailByIdFn(userInfo) {
      const res = await saveEmpDetailByIdAPI(userInfo)
      this.getUserDetailFn()
      if (!res.success) return this.$message.error(res.message)
      this.$message.success('更新成功')
    },
    // 更新-员工详情(工号/其他...)
    async updateUserInfoEV(empDeta) {
      const res = await saveEmpDetailByIdAPI(empDeta)
      this.getUserDetailFn()
      this.$message.success(res.message)
    }

  }
}
</script>

<style lang="scss" scoped>
</style>
